package com.gx.smart.building.ui.swap.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.base.BaseMVVMRefreshFragmentV2
import com.gx.smart.base.BaseViewModel
import com.gx.smart.building.admin.ScheduleAppApiService
import com.gx.smart.common.util.AppConfig
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordListQueryResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto

class SwapRecordViewModel(app: Application) : BaseViewModel(app) {
    //APP查询交接记录
    fun queryAppScheduleRecordList(
        currentPage: Int,
        beginTime: Long,
        endTime: Long
    ): LiveData<List<RecordDto>> {
        val list: MutableLiveData<List<RecordDto>> = MutableLiveData()

        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(BaseMVVMRefreshFragmentV2.PAGE_SIZE)
            .build()
        val id = SPUtils.getInstance().getString(AppConfig.ID)
        ScheduleAppApiService.getInstance().queryAppScheduleRecordList(
            comQuery,
            id,
            beginTime,
            endTime,
            object : CallBack<AppScheduleRecordListQueryResp>() {
                override fun callBack(result: AppScheduleRecordListQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        list.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return list
    }
}